package com.study.english.controller;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.study.english.context.SecurityContextHolder;
import com.study.english.domain.SysUserStudyTime;
import com.study.english.pojo.vo.SysUserVo;
import com.study.english.result.Result;
import com.study.english.service.ISysUserService;
import com.study.english.service.ISysVideoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.study.english.domain.SysUser;
import org.springframework.web.bind.annotation.*;


/**
 * @author wf
 * @description 针对表【sys_user】的数据库操作Controller
 * @createDate 2024-03-05 10:16:46
 */
@RestController
@RequestMapping("sysUser")
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private ISysVideoService sysVideoService;

    /**
     * 新增:
     */
    @PostMapping("addSysUser")
    public Result addSysUser(@RequestBody SysUserVo sysUserVo) {
        Long userId = SecurityContextHolder.getUserId();
        sysUserService.addSysUser(sysUserVo);
        return Result.success();

    }

    /**
     * 新增:
     */
    @PostMapping("login")
    public Result login(@RequestBody SysUserVo sysUserVo) throws UnsupportedEncodingException {
        SysUser sysUser = sysUserService.login(sysUserVo);
        SysUserVo sysUserVoQuery = new SysUserVo();
        BeanUtils.copyProperties(sysUser, sysUserVoQuery);
        String userJson = JSON.toJSONString(sysUserVoQuery);
        String aa = Base64.encode(userJson, "UTF-8");
        String bb = URLEncoder.encode(aa, "UTF-8");
        HashMap map = new HashMap<>();
        map.put("token", bb);
        map.put("tokenHead", "Bearer ");
        return Result.success(map);

    }

    @PostMapping("info")
    public Result info() {
        SysUserVo sysUserVo = sysUserService.info();
        return Result.success(sysUserVo);

    }

    /**
     * 退出登录
     *
     * @return
     */
    @GetMapping("loginOut")
    public Result loginOut() {
        SysUserVo sysUserVo = sysUserService.info();
        return Result.success(sysUserVo);

    }

    @GetMapping("routeList")
    public Result routeList() {
        String join = "[{\"path\": \"/level\",\"component\": \"#\",\"redirect\": \"/level/menu1/menu1-1/menu1-1-1\",\"name\": \"Level\",\"meta\": {\"title\": \"wf\",\"icon\": \"carbon:skill-level-advanced\"},\"children\": [{\"path\": \"menu1\",\"name\": \"Menu1\",\"component\": \"##\",\"redirect\": \"/level/menu1/menu1-1/menu1-1-1\",\"meta\": {\"title\": \"router.menu1\"},\"children\": [{\"path\": \"menu1-1\",\"name\": \"Menu11\",\"component\": \"##\",\"redirect\": \"/level/menu1/menu1-1/menu1-1-1\",\"meta\": {\"title\": \"router.menu11\",\"alwaysShow\": true},\"children\": [{\"path\": \"menu1-1-1\",\"name\": \"Menu111\",\"component\": \"views/Level/Menu111\",\"meta\": {\"title\": \"router.menu111\"}}]},{\"path\": \"menu1-2\",\"name\": \"Menu12\",\"component\": \"views/Level/Menu12\",\"meta\": {\"title\": \"router.menu12\"}}]},{\"path\": \"menu2\",\"name\": \"Menu2Demo\",\"component\": \"views/Level/Menu2\",\"meta\": {\"title\": \"router.menu2\"}}]}]";
        JSONArray jsonObject = JSON.parseArray(join);
        return Result.success(jsonObject);

    }

    /**
     * 分页查询:
     */
    @PostMapping("getSysUserPageList")
    public Result getSysUserPageList(@RequestBody SysUserVo sysUserVo) {
        IPage<SysUserVo> page = new Page<>(sysUserVo.getPageNum(), sysUserVo.getPageSize());
        IPage<SysUserVo> list = sysUserService.getSysUserPageList(page, sysUserVo);
        return Result.success(list);
    }


    /**
     * 不分页查询:
     */

    @PostMapping("getSysUserList")
    public Result getSysUserList(@RequestBody SysUserVo sysUserVo) {
        List<SysUserVo> sysUserVoList = sysUserService.getSysUserList(sysUserVo);

        return Result.success(sysUserVoList);
    }

    /**
     * 根据id查询详情:
     */

    @GetMapping("getSysUserById")
    public Result getSysUserById(@RequestParam String id) {
        SysUser sysUser = sysUserService.getSysUserById(id);
        return Result.success(sysUser);
    }

    /**
     * 根据id删除:
     */
    @GetMapping("delSysUser")
    public Result delSysUser(@RequestParam String id) {
        sysUserService.delSysUser(id);
        return Result.success();
    }

    /**
     * 观看时间到期后，密码校验接口
     */
    @GetMapping("checkParentPassword")
    public Result checkParentPassword(@RequestParam String password) {
        SysUserStudyTime sysUserStudyTime = sysUserService.checkParentPassword(password);
        return Result.success(sysUserStudyTime);
    }


    /**
     * 修改:
     */
    @PostMapping("updateSysUser")
    public Result updateSysUser(@RequestBody SysUserVo sysUserVo) {
        sysUserService.updateSysUser(sysUserVo);
        return Result.success();
    }

    //广告是否播放
    @GetMapping("adCheckPlay")
    public Result adCheckPlay() {
        Map map = sysUserService.adCheckPlay();
        return Result.success(map);
    }

}
